﻿package top.yangyitao.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.yangyitao.dao.ManualDao;
import top.yangyitao.model.Manual;
import top.yangyitao.model.Point;
import top.yangyitao.service.AIService;
import top.yangyitao.util.MachineLearningUtil;
import top.yangyitao.util.MonteCarlo;

@Transactional
@Service("aIService")
public class AIServiceImpl implements AIService {
	
	@Resource
	private ManualDao manualDao;;

	@Override
	public Point getBestPoint(int[][] chessBoard,int AIFlag,int userFlag) {
		Point p = MonteCarlo.getBestPoint(chessBoard, AIFlag, userFlag);
		Point p1=MachineLearningUtil.readAcknowlage(chessBoard,manualDao.getAllManual());
		if(p1!=null)
			return p1;
		return p;
	}

	@Override
	public int isWin(int[][] chessBoard, Point p) {
		int[][] shift = {{1,0},{1,1},{0,1},{1,-1}};
		for(int i=0;i<4;i++)
			if(MonteCarlo.getPointLinkLength(chessBoard, p, shift[i][0], shift[i][1], 1,false)+MonteCarlo.getPointLinkLength(chessBoard, p, shift[i][0], shift[i][1], -1,false)+1>=5)
				return chessBoard[p.getY()][p.getX()];
		return 0;
	}

	@Override
	public void saveKnowlage(String chessCode) {
		manualDao.saveManual(new Manual(chessCode));
	}

}
